//
//  VoiceItemContentViewController.m
//  ShowYork
//
//  Created by hxh on 2021/5/21.
//  Copyright © 2021 hxh. All rights reserved.
//

#import "VoiceItemContentViewController.h"
#import "VoiceItemContentHeadView.h"
#import "VoiceReadDetailReplyHeaderView.h"
#import "VoiceItemContentCell.h"
#import "VoiceReadDetailReplyItemCell.h"
@interface VoiceItemContentViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,copy) void(^scrollCallback)(UIScrollView *scrollView);
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,assign) BOOL isToolUpdate;
@property (nonatomic,strong)NSMutableArray *commentList;
@property (nonatomic,assign)NSInteger pageIndex;
@property (nonatomic,strong)NSNumber *voiceId;
@property (nonatomic,assign)BOOL noData;
@end

@implementation VoiceItemContentViewController

- (NSMutableArray *)commentList{
    if (!_commentList) {
        _commentList = [NSMutableArray array];
    }
    return _commentList;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.pageIndex = 1;
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    self.tableView.estimatedRowHeight = 44 ;  // 44为任意值
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedSectionFooterHeight = .0f;
    self.tableView.estimatedSectionHeaderHeight = .0f;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.separatorColor = [UIColor clearColor];
    [self.view addSubview:self.tableView];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(self.view);
    }];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateContentOffset:) name:@"updateContentOffset" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateVoiceCommentList:) name:@"updateVoiceCommentList" object:nil];
    
    __weak typeof(self)weakSelf = self;
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        weakSelf.pageIndex++;
        [weakSelf loadCommentList];
    }];
    
    [footer setTitle:@"—— 没有更多了 ——" forState:MJRefreshStateNoMoreData];
    footer.stateLabel.font = font(10);
    footer.stateLabel.textColor = RGBA(153, 153, 153, 1);
    
    self.tableView.mj_footer = footer;
    
    
}

- (void)loadCommentList{
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"md5"] = [RPAppManager md5StrWithApi:PostNewCommentList];
    param[@"projectId"] = self.voiceId;
    param[@"userId"] = [RPAppManager getCustomerId];
    param[@"type"] = @(1);
    param[@"pageIndex"] = @(1);
    param[@"pageSize"] = @10;
    [[RPNetworkExtensionTool sharedTool] requestPOST:kGET_REQUEST_URL(PostNewCommentList) parames:param success:^(id responseObj) {
        [self.commentList addObjectsFromArray:[VoiceReadCommentListModel mj_objectArrayWithKeyValuesArray:responseObj[RESPONSE][LIST]]];
     
        self.noData = ([responseObj[RESPONSE][LASTPAGE]boolValue] || ![responseObj[RESPONSE][NEXTPAGE]boolValue]);
            
        [self setFooterBottom];
        
        [self.tableView reloadSection:2 withRowAnimation:UITableViewRowAnimationNone];
    
    } failure:^(id error) {
         
    }];
}

- (void)setFooterBottom{
    CGFloat bottom = self.tableView.mj_insetB;
    if (self.noData) {
        [self.tableView.mj_footer endRefreshingWithNoMoreData];
        if (self.tableView.mj_footer.cl_height == 44.0f) {
            self.tableView.mj_footer.cl_height = 138.0f;
            if (bottom == 44.0f || bottom == GK_SAVEAREA_BTM + 44.0f) {
                self.tableView.mj_insetB = 138.0f;
            }
        }
    }else{
        [self.tableView.mj_footer resetNoMoreData];
        if (self.tableView.mj_footer.cl_height == 138.0f) {
            self.tableView.mj_footer.cl_height = 44.0f;
            if (bottom == 138.0f) {
                self.tableView.mj_insetB = 44.0f;
            }
        }
    }
}

- (void)updateVoiceCommentList:(NSNotification *)note{
    self.commentList = note.userInfo[@"list"];
    self.voiceId = note.userInfo[@"voiceId"];
    [self.tableView reloadData];
}

- (void)updateContentOffset:(NSNotification *)note{
    
    self.isToolUpdate = YES;

    NSInteger index = [note.userInfo[@"index"] intValue];

    [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:index] atScrollPosition:UITableViewScrollPositionTop animated:YES];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 2) {
        return self.commentList.count;
    }
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0 || indexPath.section == 1) {
        VoiceItemContentCell *cell = [VoiceItemContentCell cellWithTableView:tableView];
        return cell;
    }
    VoiceReadDetailReplyItemCell *cell = [VoiceReadDetailReplyItemCell cellWithTableView:tableView];
    [cell setModel:self.commentList[indexPath.item]];

    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 44.0f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01f;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        VoiceItemContentHeadView *v = [[VoiceItemContentHeadView alloc]init];
        [v setItemTitle:@"朗读"];
        return v;
    }else if (section == 1){
        VoiceItemContentHeadView *v = [[VoiceItemContentHeadView alloc]init];
        [v setItemTitle:@"鉴赏"];
        return v;
    }else{
        VoiceReadDetailReplyHeaderView *v = [[VoiceReadDetailReplyHeaderView alloc]init];
        return v;
    }
    return nil;
}


- (UIView *)listView {
    return self.view;
}

- (UIScrollView *)listScrollView {
    return self.tableView;
}


- (void)listViewDidScrollCallback:(void (^)(UIScrollView *))callback {
    self.scrollCallback = callback;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    !self.scrollCallback ?: self.scrollCallback(scrollView);
    
    if (self.isToolUpdate) {
        return;
    }

    CGRect rect = [self.tableView rectForHeaderInSection:0];
    CGRect other1 = [self.tableView rectForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    
    if (scrollView.contentOffset.y <= CGRectGetMaxY(other1) && scrollView.contentOffset.y >= rect.origin.y) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"updateToolBarAtFirst" object:nil];
    }
    CGRect rect1 = [self.tableView rectForHeaderInSection:1];
    CGRect other2 = [self.tableView rectForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]];
    if (scrollView.contentOffset.y <= CGRectGetMaxY(other2) && scrollView.contentOffset.y >= rect1.origin.y) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"updateToolBarAtSecond" object:nil];
    }
    CGRect rect2 = [self.tableView rectForHeaderInSection:2];
    if (scrollView.contentOffset.y <= CGRectGetMaxY(rect2) && scrollView.contentOffset.y >= rect2.origin.y) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"updateToolBarAtLast" object:nil];
    }
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    self.isToolUpdate = NO;
}

@end
